package io.gitee.yaoshanli.engine.service.impl;

import io.gitee.yaoshanli.engine.db.JwDbHelper;
import io.gitee.yaoshanli.engine.domain.UserModel;
import io.gitee.yaoshanli.engine.service.INodeService;
import io.gitee.yaoshanli.engine.utils.StringUtils;
import io.gitee.yaoshanli.engine.model.WfNodeModel;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;

public class NodeService implements INodeService {
    @Override
    public WfNodeModel getNode(String id) {
        String sql="select * from jw_wf_node where id=?";
        Object[] args = new Object[]{id};
        WfNodeModel wfNodeModel= JwDbHelper.queryFirst(WfNodeModel.class,sql,args);
        return wfNodeModel;
    }

    @Override
    public boolean remove(String id) {
        String sql="delete from jw_wf_node where id=?";
        Object[] args = new Object[]{id};
        return JwDbHelper.update(sql,args)>0;
    }

    @Override
    public boolean removeNodes(String wf_id) {
        String sql="delete from jw_wf_node where wf_id=?";
        Object[] args = new Object[]{wf_id};
        return JwDbHelper.update(sql,args)>0;
    }

    @Override
    public boolean saveOrUpdate(WfNodeModel wfNode) {
        String sql="select * from jw_wf_node where id=?";
        WfNodeModel wfNodeModel=JwDbHelper.queryFirst(WfNodeModel.class,sql,wfNode.getId());
        if(wfNodeModel!=null)
        {
            sql="update jw_wf_node set detail_url = ?,deal_action = ?,app_url = ?,deal_back = ?,deal_attr = ?,deal_type = ?,deal_type_rate = ?,by_group = ?,is_back = ? where id=?";
            Object args[]=new Object[]{wfNode.getDetail_url(),wfNode.getDeal_action(),wfNode.getApp_url(),
                    wfNode.getDeal_back(),wfNode.getDeal_attr(),wfNode.getDeal_type(),wfNode.getDeal_type_rate(), wfNode.getBy_group(), wfNode.getIs_back(),wfNode.getId()};
            return JwDbHelper.update(sql,args)>0;
        }
        else
        {
            String id=wfNode.getId();
            if(StringUtils.isEmpty(id))
            {
                id=StringUtils.GUID();
            }
            sql="insert into jw_wf_node(id,wf_id,name,position_x,position_y,width,height,shape,detail_url,deal_action," +
                    "app_url,deal_back,deal_attr,deal_type,deal_type_rate,by_group,is_back)" +
                    " values (?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            Object args[]=new Object[]{id,wfNode.getWf_id(),wfNode.getName(),wfNode.getPosition_x(),wfNode.getPosition_y(),
            wfNode.getWidth(),wfNode.getHeight(),wfNode.getShape(),wfNode.getDetail_url(),wfNode.getDeal_action(),wfNode.getApp_url(),
            wfNode.getDeal_back(),wfNode.getDeal_attr(),wfNode.getDeal_type(),wfNode.getDeal_type_rate(), wfNode.getBy_group(), wfNode.getIs_back()};
            return JwDbHelper.update(sql,args)>0;
        }
    }

    @Override
    public int getUserCount(String id) {
        WfNodeModel node=getNode(id);
        //String group=node.getBy_group();
        //获取总人数
        return getUserByGroup(node).size();
    }

    /**
     * 利用反射机制
     *  获取节点的人员
     * @param node
     * @return
     */
    public List<UserModel> getUserByGroup(WfNodeModel node)
    {
        String by_group=node.getBy_group();
        int is_filter=node.getIs_filter();
        String sql="select reflect_class from jw_wf_group where value=?";
        Object args[]=new Object[]{by_group};
        String classPath = JwDbHelper.queryString(sql,args);
        if(StringUtils.isEmpty(classPath))
        {
            return null;
        }

        Class c = null;
        try {
            c = Class.forName(classPath);

            //传递当前节点的 人员组代码
            Method setGroup = c.getMethod("setGroup", String.class);
            Constructor constructor = c.getConstructor();
            Object o = constructor.newInstance();
            setGroup.invoke(o,by_group);

            //传递当前节点的 人员过滤条件
            Method setFilter = c.getMethod("setIs_filter", Integer.class);
            Constructor constFilter = c.getConstructor();
            Object ofilter = constFilter.newInstance();
            setFilter.invoke(ofilter,is_filter);

            //获取当前节点的人员信息列表
            Method getUsers = c.getMethod("getUsers");
            List<UserModel> users= (List<UserModel>) getUsers.invoke(o);
            return users;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
